package com.atguigu.gmall.item.controller;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.client.ProductFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;


/**
 * @Author: 孙润达
 * @Date: 2022/1/8 13:24
 */
@Api(tags= "商品详情数据汇总")
@RestController
@RequestMapping("/api/item")
public class ItemApiController {
    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("/getItem/{skuId}")
    public Map getItem(@ApiParam(value = "库存id") @PathVariable(name = "skuId") Long skuId){
        Map map  = new HashMap<>();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //3根据skuId获取sku信息,根据skuId 查询图片列表集合
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //1通过三级分类id查询分类信息
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        });
        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //4根据spuId，查询spu_sale_attr_value（被选中的）
            List<SpuSaleAttr> spuSaleAttrList =
                    productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        });

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //5根据spuId，skuId 查询销售属性集合
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            map.put("valuesSkuJson", JSONObject.toJSONString(skuValueIdsMap));
        });
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //2获取sku最新价格
            BigDecimal price = productFeignClient.getSkuPrice(skuId);
            map.put("price", price);
        }, threadPoolExecutor);

        //远程调用加分更新热点
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId, 500);
        }, threadPoolExecutor);


        CompletableFuture.allOf(
                priceCompletableFuture,
                valuesSkuJsonCompletableFuture,
                spuSaleAttrListCompletableFuture,
                categoryViewCompletableFuture,
                voidCompletableFuture
        ).join();
        return map;

       /* //3根据skuId获取sku信息,根据skuId 查询图片列表集合
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        map.put("skuInfo",skuInfo);

        //1通过三级分类id查询分类信息
        BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
        map.put("categoryView",categoryView);
        //2获取sku最新价格
        BigDecimal price = productFeignClient.getSkuPrice(skuId);
        map.put("price",price);
        //4根据spuId，查询spu_sale_attr_value（被选中的）
        List<SpuSaleAttr> spuSaleAttrList =
                productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
        map.put("spuSaleAttrList",spuSaleAttrList);
        //5根据spuId，skuId 查询销售属性集合
        Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
        map.put("valuesSkuJson",JSONObject.toJSONString(skuValueIdsMap));
        return map;*/
    }
}
